package com.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.config.exception.GlobalExcetion;
import com.example.system.domain.SysTableScript;
import com.example.system.service.SysTableScriptService;
import com.example.system.mapper.SysTableScriptMapper;
import com.example.system.vo.SysTableScriptVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
* @author CETC
* @description 针对表【sys_table_script】的数据库操作Service实现
* @createDate 2025-10-25 10:39:12
*/
@Service
public class SysTableScriptServiceImpl extends ServiceImpl<SysTableScriptMapper, SysTableScript>
    implements SysTableScriptService{

    @Override
    public SysTableScript saveOrUpdateSysTableScript(SysTableScriptVo vo) throws GlobalExcetion {
        if(vo.getUuid()==null){
            SysTableScript table=this.getSysTableScriptByNo(vo.getTableNo());
            if(table!=null){
                throw new GlobalExcetion(1,"表名编号已存在");
            }
            table=this.getSysTableScriptByTableName(vo.getTableName());
            if(table!=null){
                throw new GlobalExcetion(1,"表名已存在");
            }
            SysTableScript sysTableScript = new SysTableScript();
            BeanUtils.copyProperties(vo,sysTableScript);
            sysTableScript.setIsDeleted(0);
            this.save(sysTableScript);
            return sysTableScript;
        }else {
            SysTableScript table=this.getById(vo.getUuid());
            if(table==null){
                throw new GlobalExcetion(1,"系统错误，请刷新后重试");
            }
            if(!vo.getTableName().equals(table.getTableName())){
                SysTableScript sysTableScript = this.getSysTableScriptByTableName(vo.getTableName());
                if(sysTableScript!=null){
                    throw new GlobalExcetion(1,"表名已存在");
                }
            }
            if(!vo.getTableNo().equals(table.getTableNo())){
                SysTableScript sysTableScript = this.getSysTableScriptByNo(vo.getTableNo());
                if(sysTableScript!=null){
                    throw new GlobalExcetion(1,"表名编号已存在");
                }
            }
            BeanUtils.copyProperties(vo,table);
            this.updateById(table);
            return table;
        }
    }

    @Override
    public SysTableScript getSysTableScriptByNo(String no) {
        LambdaQueryWrapper<SysTableScript>  lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysTableScript::getTableNo,no);
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public SysTableScript getSysTableScriptByTableName(String tableName) {
        LambdaQueryWrapper<SysTableScript>  lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysTableScript::getTableName,tableName);
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<SysTableScript> getByServeName(String serveName) {
        LambdaQueryWrapper<SysTableScript> scriptLambdaQueryWrapper=new LambdaQueryWrapper<>();
        scriptLambdaQueryWrapper.like(SysTableScript::getServerName,serveName);
        return this.list(scriptLambdaQueryWrapper);
    }
}




